PlaceDetailPortlet = Class.extend({
	init: function()	{
		this.name = "PlaceDetailPortlet";
	},
	
	onReloadPage: function()	{
		this.run();
	},
	
	onBegin: function()	{
		this.map = new Map();
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	markLocation: function(position, txt){
		this.map.markLocation(position, txt);
		this.map.deleteAllMarker();
	},
	
	run: function()	{
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('place', 'get-place-detail', {id: id}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
				obj.map.setMap("PlaceDetailPortlet-Map", {
					zoom: 16,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					disableDoubleClickZoom: true
				});
//				obj.map.locate(function(position, txt) {
//					obj.markLocation(position, txt);
//				});
				obj.map.focus(ret.lat,ret.lng);
				obj.map.mark(ret.lat,ret.lng, ret.name);
				obj.map.deleteAllMarker();
				obj.getNearPlaces();
				var vote = '';
				if(obj.model.check_voted==0)
					vote = '<br /><span id="vote-'+id+'" class="vote">\
							<span class="star_vote"  onclick="generateEvent(\'VotePlace\', {value:1})"></span>\
							<span class="star_vote"  onclick="generateEvent(\'VotePlace\', {value:2})"></span>\
							<span class="star_vote"  onclick="generateEvent(\'VotePlace\', {value:3})"></span>\
							<span class="star_vote"  onclick="generateEvent(\'VotePlace\', {value:4})"></span>\
							<span class="star_vote"  onclick="generateEvent(\'VotePlace\', {value:5})"></span>\
							</span>';
				var checkin = '';
				if(obj.model.check_checkedin==0)
					checkin = '<br /><a class="checkin" onclick="generateEvent(\'Checkin\')">Đến đây</a>';
				else if(obj.model.check_checkedin==1)
					checkin = '<br />Bạn đang ở đây. <a class="checkin" onclick="generateEvent(\'Checkout\')">Rời khỏi</a>';
				obj.map.show(ret.lat, ret.lng, ret.name + checkin + vote);
			}
		});
	},
	getNearPlaces: function(){
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('place', 'get-near-places', {id:id}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.map.showPlaceOnMap(obj.model);
			}
		});
	},
	onVotePlace: function(eventData){
		var obj = this;
		var id = this.getRequest().getParam('id');
		var subject = SingletonFactory.getInstance(Subject);
		this.onAjax('place', 'vote', {id: id, value: eventData.value}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Bình chọn thành công!');
				subject.notifyEvent('ReloadPage');
			},
			
			'onFailure': function(msg){
				alert(msg);
			}
		});
	},
	onCheckin: function(){
		var obj = this;
		var id = this.getRequest().getParam('id');
		var subject = SingletonFactory.getInstance(Subject);
		this.onAjax('user', 'checkin', {place_id: id}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Đến địa điểm thành công!');
				subject.notifyEvent('ReloadPage');
			},
			
			'onFailure': function(msg){
				alert(msg);
			}
		});
	},
	onCheckout: function(){
		var obj = this;
		var id = this.getRequest().getParam('id');
		var subject = SingletonFactory.getInstance(Subject);
		this.onAjax('user', 'checkout', {place_id: id}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Rời khỏi địa điểm thành công!');
				subject.notifyEvent('ReloadPage');
			},
			
			'onFailure': function(msg){
				alert(msg);
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);